#include "stdafx.h"
#include "Explorer.h"
#include "PluginInfo.h"
#include "AccelBase.h"
#include "ExplorerFrm.h"

#include <gtl/module/IPlugin.h>
#include <gtl/module/ISoftBus.h>

CPluginInfo::VectorPluginInfo CPluginInfo::m_vecPluginInfo;

void CPluginInfo::LoadPlugin()
{
	WORD wCommand = ID_AccelPlugin_Base;
	CExplorerApp* pExplorerApp = static_cast<CExplorerApp*>(AfxGetApp());
	ISoftBus* pISoftBus = pExplorerApp->GetISoftBusPtr();
	if(pISoftBus == NULL)
		return;

	uint32 nModuleCount = pISoftBus->GetModuleCount();
	for(uint32 i = 0; i < nModuleCount; i++)
	{
		IPlugin* pIPlugin = pExplorerApp->QI<IPlugin>(i);
		if(pIPlugin == NULL)
			continue;

		uint32 nPluginCount = 0;
		LPPluginInfo lpPluginInfo = pIPlugin->GetInfo(nPluginCount);
		for(uint32 j = 0; j < nPluginCount; j++)
		{
			const SPluginInfo& sPluginInfo = lpPluginInfo[j];
			const SPluginInfo::SShortcutKey& key = sPluginInfo.shkey;

			m_vecPluginInfo.push_back(plugin());
			plugin& plug = m_vecPluginInfo.back();
			plug.pIPlugin = pIPlugin;
			plug.strName = sPluginInfo.name;
			plug.strDesc = (LPCTSTR)sPluginInfo.description;
			plug.version = sPluginInfo.version;
			plug.index = j;
			plug.wVirtKey = CAccelBase::GetVirtKey(key.ctrl, key.shift, key.alt);
			plug.wKey = key.key;
			plug.wCommand = wCommand++;
		}
	} // end for
}

void CPluginInfo::ShowPlugin(WORD wCommand, CExplorerFrm* pExplorerFrm)
{
	for(size_t i = 0; i < m_vecPluginInfo.size(); i++)
	{
		plugin& plug = m_vecPluginInfo[i];
		if(plug.wCommand != wCommand)
			continue;

		if(plug.pIPlugin == NULL)
			break;

		std::vector<CString> vecSelectedPathNames;
		pExplorerFrm->GetSelectedPathNames(vecSelectedPathNames);
		if(vecSelectedPathNames.empty())
			break;

		plug.pIPlugin->Show(plug.index, (uint32)(LPCTSTR)vecSelectedPathNames.front());
		break;
	}
}
